﻿#pragma execution_character_set("utf-8")
#include "filedata.h"
#include "globaldata.h"
#include "hmicommondefine.h"
#include "instructionEnum.h"

extern FileData fileData;

GlobalData::GlobalData()
{
    onePageAxisCount=6;//手动一页６个轴
    serverIP = "";                  //服务器IP+PORT

    gsoapCtrlState = EM_UPDATE_STATUS_LV1;     //初始化 soap_call使用状态

    initCtrlState = EM_UPDATE_STATUS_LV1;      //初始化 控制器使用状态(表明控制器是否初始化完成)

    initHmiState = EM_UPDATE_STATUS_LV1;

    mountSuccess = false;

    controllerIsConnected = EM_CONNECT_END;//初始化 连接状态

    globalLoginStatus = false;      //初始化 登陆状态

    currentTime = "";               //初始化 当前时间

    globalRegStatus = 0;            //初始化注册码状态(没有状态)

    currentUseAxis = 0;             //默认当前使用第一个轴

    programRunAllow = false;        //默认不允许

    ioUpdateFlag = E_UPDATA_TYPE_NONE;               //默认 没有允许的刷新 2是LV2 3是LV3

    usbFlag = false;                //没有usb

    currentRobotID = 1;             //初始化机器人ID(没有数据状态,有数据为1001之类的)//2017-02-25改动,需要测试
    currentRobotFileAddress = "";
//    currentRobotPicAddress = "";
    defaultUserFile = "";           //缺省文件
    jogPageIndex = 0;
    isFtpConnectSuccess = false;
    isUseFtpOrNfs = false;
    ftpUserName = "";
    ftpUserPasswd = "";
    ftpPort = 21;
    fileData.globalData = this;
    isUpdatePointCloud=true;
}

int GlobalData::setCurrentRobotId(int idIn)
{
    currentRobotID=idIn;
    return 1;
}

std::string GlobalData::getCurrentRobotName()
{
    std::string tmpName;
    lock();
    for(int i=0;i<robotInfoList.size();i++)
    {
        if(currentRobotID==robotInfoList[i].robotId)
        {
            tmpName=robotInfoList[i].robotName;
            break;
        }
    }
    unlock();
    return tmpName;
}

std::string GlobalData::getCurrentRobotPictureAddress()
{
    std::string tmpName;
    lock();
    for(int i=0;i<robotInfoList.size();i++)
    {
        if(currentRobotID==robotInfoList[i].robotId)
        {
            tmpName=robotInfoList[i].robotPictureAddress;
            break;
        }
    }
    unlock();
    return tmpName;
}

int GlobalData::setUserPassword(std::string passwordIn)
{
    lock();
    userInfo.password=passwordIn;
    unlock();
    return 1;
}

int GlobalData::getMotorInfo(std::vector<double> &axisTorqueOut, std::vector<double> &axisSpeedOut,
                             std::vector<int> &axisPositionOut, std::vector<int> &axisFollowOffsetOut)
{
    lock();
    axisTorqueOut=axisTorque;
    axisSpeedOut=axisSpeed;
    axisPositionOut=axisPosition;
    axisFollowOffsetOut=axisFollowOffset;
    unlock();
    return 1;
}

std::string GlobalData::getSerialNumber()
{
    std::string tmpUser;
    lock();
    tmpUser=serialNumber;
    unlock();
    return tmpUser;
}

std::string GlobalData::getGlobalLockMachTime()
{
    std::string tmpUser;
    lock();
    tmpUser=globalLockMachTime;
    unlock();
    return tmpUser;
}

int GlobalData::getVersionList(std::vector<ComponentVersion> &componentVersionListOut)
{
    lock();
    componentVersionListOut=componentVersionList;
    unlock();
    return 1;
}

int GlobalData::getControllerTimeHistroy(ControllerTimeHistory &timeHistoryOut)
{
    lock();
    timeHistoryOut=timeHistory;
    unlock();
    return 1;
}

int GlobalData::getRobotInfoListLv3(std::vector<CommRobotInfo> &robotInfoListLv3Out)
{
    lock();
    robotInfoListLv3Out=robotInfoListLv3;
    unlock();
    return 1;
}

int GlobalData::getAxisUnitAndLimit(std::vector<std::vector<float> > &axisLimitOut, std::vector<int> &axisUnitOut)
{
    lock();
    axisLimitOut=axisLimit;
    axisUnitOut=axisUnit;
    unlock();
    return 1;
}

//std::string GlobalData::getCurrentRobotPicAddress()
//{
//    std::string tmpUser;
//    lock();
//    tmpUser=currentRobotPicAddress;
//    unlock();
//    return tmpUser;
//}

int GlobalData::getRobotInfoList(std::vector<CommRobotInfo> &robotInfoListOut)
{
    lock();
    robotInfoListOut=robotInfoList;
    unlock();
    return 1;
}

QString GlobalData::getServerIp()
{
    QString tmpUser;
    lock();
    tmpUser=serverIP;
    unlock();
    return tmpUser;
}

int GlobalData::getFtp(QString &ftpUserNameOut, QString &ftpUserPasswdOut, int &portOut)
{
    lock();
    ftpUserNameOut=ftpUserName;
    ftpUserPasswdOut=ftpUserPasswd;
    portOut=ftpPort;
    unlock();
    return 1;
}

int GlobalData::setFtp(QString ftpUserNameIn, QString ftpUserPasswdIn, int portIn)
{
    lock();
    ftpUserName=ftpUserNameIn;
    ftpUserPasswd=ftpUserPasswdIn;
    ftpPort=portIn;
    unlock();
    return 1;
}

int GlobalData::getVehicleBasicInfo(VehicleBasicInfo &infoOut)
{

    lock();
    infoOut=vehicleBasicInfo;
    unlock();
    return 1;
}

int GlobalData::getCurrentRunFile(CurrentRunFile &currentRunFileOut)
{
    lock();
    currentRunFileOut=currentRunFile;
    unlock();
    return 1;
}

int GlobalData::setDefaultUserProgramFileName(std::string nameIn)
{
    lock();
    defaultUserFile=nameIn;
    unlock();
    return 1;
}

int GlobalData::setServerIp(QString serverIPIn)
{
    lock();
    serverIP=serverIPIn;
    serverIPArray = serverIPIn.toLatin1();
    unlock();
    return 1;
}

std::string GlobalData::getDefaultUserProgramFileName()
{
    std::string tmpUser;
    lock();
    tmpUser=defaultUserFile;
    unlock();
    return tmpUser;

}

std::string GlobalData::getCurrentTime()
{
    std::string tmpUser;
    lock();
    tmpUser=currentTime;
    unlock();
    return tmpUser;

}

UserInfo GlobalData::getUserInfo()
{
    UserInfo tmpUser;
    lock();
    tmpUser=userInfo;
    unlock();
    return tmpUser;
}

int GlobalData::setUserInfo(UserInfo userInfoIn)
{
    lock();
    userInfo=userInfoIn;
    unlock();
    return 1;
}

int GlobalData::getCurrentArmPosition_decart(PointPro &currentPositionDecartOut)
{
    lock();
    currentPositionDecartOut=currentPositionWorld;
    unlock();
    return 1;
}

int GlobalData::getCurrentArmPosition_joint(PointPro &currentPositionJointOut)
{
    lock();
    currentPositionJointOut=currentPositionJoint;
    unlock();
    return 1;
}

int GlobalData::getAiAoSiSo(    std::vector<bool> &ioDI,std::vector<bool> &ioDo,std::vector<double> &ioai,
                                std::vector<double> &ioao,std::vector<bool> &iosi,std::vector<bool> &ioso)
{
    lock();
    ioDI=ioIn;
    ioDo=ioOut;
    ioai=ai;
    ioao=ao;
    iosi=si;
    ioso=so;
    unlock();
    return 1;
}

int GlobalData::getPlRegister(std::vector<PlRegStruct> &registerOut)
{
    lock();
    registerOut=plRegister;
    unlock();
    return 1;
}

int GlobalData::getTimerRegister(std::vector<double> &registerOut)
{
    lock();
    registerOut=timerRegister;
    unlock();
    return 1;
}

int GlobalData::getGlobalRegister(std::vector<double> &registerOut)
{
    lock();
    registerOut=globalRegister;
    unlock();
    return 1;
}

int GlobalData::getLocalRegister(std::vector<double> &registerOut)
{
    lock();
    registerOut=localRegister;
    unlock();
    return 1;
}

int GlobalData::getPointRegister(std::vector<PointPro> &registerOut)
{
    lock();
    registerOut=pointRegister;
    unlock();
    return 1;
}

int GlobalData::setUpdatePointCloud(bool isEnable)
{
    isUpdatePointCloud=isEnable;
    return 1;
}

bool GlobalData::isUpdatePointCloudStatus()
{
    return isUpdatePointCloud;
}

//int GlobalData::setDefaultProgramFile(std::string &defaultUserFile)
//{
//            defaultUserFile = defaultUserFile;
//            return 1;
//}

int GlobalData::setCoordinateInfo(std::vector<HmiToolCoordinateInformation> &recvToolCoordinate,
                                  std::vector<HmiUserCoordinateInformation> &recvUserCoordinate)
{
    lock();
    fileData.toolInfo.clear();
    for(int i = 0;i<recvToolCoordinate.size();i++)
    {
        fileData.toolInfo.append(recvToolCoordinate[i]);

        //qDebug()<<"getToolCoordinateSlot index= "<<recvToolCoordinate[i].index;
    }

    fileData.userInfo.clear();
    for(int i = 0;i<recvUserCoordinate.size();i++)
    {
        fileData.userInfo.append(recvUserCoordinate[i]);

        //qDebug()<<"getUserCoordinateSlot index= "<<recvUserCoordinate[i].index;
    }

    unlock();
    return 1;
}

int GlobalData::setVehicleBasicInfo(VehicleBasicInfo &vehicleInfo)
{
    lock();
    //agv
    vehicleBasicInfo=vehicleInfo;
    vmarkInWorld=vehicleInfo.vmarkInWorld;
    vehicleInVmark=vehicleInfo.vehicleInVmark;
    vehicleInQrcode=vehicleInfo.vehicleInQrcode;
    vehicleInCU1=vehicleInfo.vehicleInCU1;
    vehicleInWorld=vehicleInfo.vehicleInWorld;
    mapName=vehicleInfo.currentMapName;
    unlock();

    return 1;
}

int GlobalData::setUpdateLv1(UpdateDataLv1 &updateDataLv1)
{

    lock();
    globalRegStatus = updateDataLv1.globalRegStatus;

    serialNumber = updateDataLv1.serialNumber;

    registerNumber = updateDataLv1.registerNumber;

    globalLockMachTime = updateDataLv1.globalLockMachTime;

    mainProgramVersion = updateDataLv1.mainProgramVersion;

    componentVersionList = updateDataLv1.componentVersionList;

    std::vector<struct AxisLimit>::iterator iter2;
    axisLimit.clear();
    int i = 0;//
    std::vector <float> tmpLimit;
    tmpLimit.resize(2);
    for(iter2 = updateDataLv1.axisLimit.begin(); iter2 != updateDataLv1.axisLimit.end(); iter2++)
    {

        tmpLimit[0] =  iter2->negative;
        tmpLimit[1] =  iter2->positive;
        axisLimit.push_back(tmpLimit);
        i++;
    }
    //qDebug()<<axisLimit.size();

    axisUnit = updateDataLv1.axisUnit;      //本体轴单位
    fileData.saveAxisUnit.clear();
    for(int i = 0;i <axisUnit.size();i++)
    {
        fileData.saveAxisUnit.append(axisUnit[i]);
    }

    robotAxisCount = updateDataLv1.robotAxisCount;

    extAxisCount = updateDataLv1.extAxisCount;

    extAxisUnit = updateDataLv1.extAxisUnit;        //外部轴单位 lv1
    fileData.saveExtAxisUnit.clear();
    for(int i = 0;i <extAxisUnit.size();i++)
    {
        fileData.saveExtAxisUnit.append(extAxisUnit[i]);
    }

    extAxisLimit = updateDataLv1.extAxisLimit;      //外部轴限位
    axisLogicalType = updateDataLv1.axisType;      //轴逻辑类型
    extAxisLogicalType = updateDataLv1.extAxisType; //轴逻辑类型

    //test
//        extAxisCount = 4;
//        AxisLimit tmp;tmp.positive = -100;tmp.negative = 100;
//        for(int i =0;i<4;i++)
//        {
//            extAxisUnit.push_back(1);
//            extAxisLimit.push_back(tmp);
//        }
    //test end

    controllerIp = updateDataLv1.controllerIp;

    controllerMac = updateDataLv1.controllerMac;

    //机器人信息 2020.12.12 补充
    robotInfoList = updateDataLv1.robotInfoList;
    qDebug()<<"LV1 ROBOT SIZE = "<<robotInfoList.size();
//        for(int i = 0; i <robotInfoList.size() ;i++)
//        {
//            qDebug()<<"LV1 ROBOT ID   "<<robotInfoList[i].robotId;
//            qDebug()<<"LV1 ROBOT NAME "<<QString::fromStdString(robotInfoList[i].robotName);
//            qDebug()<<"LV1 ROBOT FILE "<<QString::fromStdString(robotInfoList[i].robotFileAddress);
//            qDebug()<<"LV1 ROBOT PIC  "<<QString::fromStdString(robotInfoList[i].robotPictureAddress);
//        }

    if(0 == currentRobotID)//初始状态
    {
        if(robotInfoList.size() > 0)
        {
            currentRobotID = robotInfoList[0].robotId;
            currentRobotFileAddress = robotInfoList[0].robotFileAddress;
//            currentRobotPicAddress = robotInfoList[0].robotPictureAddress;
            fileData.currentRobotFileAddress = QString::fromStdString(robotInfoList[0].robotFileAddress);
            qDebug()<<"ID == 0 ***"<<fileData.currentRobotFileAddress;
        }
    }
    else//切换机器人的时候
    {
        for(int i = 0; i <robotInfoList.size() ;i++)
        {
            if(currentRobotID == robotInfoList[i].robotId)
            {
                currentRobotFileAddress = robotInfoList[i].robotFileAddress;
//                currentRobotPicAddress = robotInfoList[i].robotPictureAddress;
                fileData.currentRobotFileAddress = QString::fromStdString(robotInfoList[i].robotFileAddress);

                qDebug()<<"ID == "<<robotInfoList[i].robotId<<" ***"<<fileData.currentRobotFileAddress;

                break;
            }
            if(i == robotInfoList.size()-1)
            {
                //获取失败
                currentRobotID = robotInfoList[0].robotId;
                currentRobotFileAddress = robotInfoList[0].robotFileAddress;
//                currentRobotPicAddress = robotInfoList[0].robotPictureAddress;
                fileData.currentRobotFileAddress = QString::fromStdString(robotInfoList[0].robotFileAddress);
                qDebug()<<"get false :: ID == "<<robotInfoList[0].robotId<<" ***"<<fileData.currentRobotFileAddress;
            }
        }
    }
    unlock();
    return 1;
}

int GlobalData::setUpdateLv2(UpdateDataLv2 &updateDataLv2)
{
    lock();
    //机器人相关参数数据
    globalSafePointFlag = updateDataLv2.globalSafePointFlag;

    globalHandSwitch = updateDataLv2.globalHandSwitch;

    globalProSpeed = updateDataLv2.globalProSpeed;

    globalManSpeed = updateDataLv2.globalManSpeed;

    globalHandRunStatus = updateDataLv2.globalHandRunStatus;

    globalZeroCtrlFlag = updateDataLv2.globalZeroCtrlFlag;

    globalCoordinate = updateDataLv2.globalCoordinate;

    globalCurrentState = updateDataLv2.globalCurrentState;
    globalCurrentState_motion = updateDataLv2.globalCurrentState_motion;

    globalHandState = updateDataLv2.globalHandState;

    globalServoState = updateDataLv2.globalServoState;

    globalZeroStatus = updateDataLv2.globalZeroStatus;

    emergencyStatus = updateDataLv2.emergencyStatus;

    globalPosType = updateDataLv2.globalPosType;

    globalDefaultUserNum = updateDataLv2.globalDefaultUserNum;

    globalDefaultToolNum = updateDataLv2.globalDefaultToolNum;

    startType = updateDataLv2.startType;

    //当前点数据
    currentPositionJoint = updateDataLv2.currentPositionJoint;
    currentPositionWorld = updateDataLv2.currentPositionWorld;

    //时间部分
    currentTime = updateDataLv2.currentTime;

    //轴数据
    axisTorque = updateDataLv2.axisTorque;
    axisSpeed = updateDataLv2.axisSpeed;
    axisPosition = updateDataLv2.axisPosition;
    axisFollowOffset = updateDataLv2.axisFollowOffset;

    //文件数据
    currentRunFile = updateDataLv2.currentRunFile;

    //产量
    currentProductivity=updateDataLv2.currentProductivity;
    accumulateProductivity=updateDataLv2.accumulateProductivity;
    workpieceRatio=updateDataLv2.workpieceRatio;

    mainProgramFileVersion=updateDataLv2.mainProgramFileVersion;
    currentRobotID=updateDataLv2.currentRobotID;
    unlock();
    return 1;
}

int GlobalData::setUpdateLv3(UpdateDataLv3 &updateDataLv3)
{
    lock();
    //IO数据(以后撤离)
    ioIn = updateDataLv3.ioIn;
    ioOut = updateDataLv3.ioOut;

    ai = updateDataLv3.ai;
    ao = updateDataLv3.ao;

    si = updateDataLv3.si;
    so = updateDataLv3.so;

    localRegister = updateDataLv3.localRegister;
    globalRegister = updateDataLv3.globalRegister;

    timerRegister = updateDataLv3.timerRegister;

    pointRegister = updateDataLv3.pointRegister;

    plRegister = updateDataLv3.plRegister;

    //时间部分
    timeHistory.powerOnTime = updateDataLv3.powerOnTime;
    timeHistory.totalPowerOnTime = updateDataLv3.totalPowerOnTime;
    timeHistory.totalStartOnTime = updateDataLv3.totalStartMachineOnTime;
    timeHistory.servoOnTime = updateDataLv3.servoOnTime;

    robotInfoListLv3 = updateDataLv3.robotInfoList;
    unlock();
    return 1;
}

int GlobalData::setMessageVector(std::vector<Message> &messageVectorIn)
{
    lock();
    messageVector=messageVectorIn;
    unlock();
    return 1;
}

int GlobalData::getMessageVector(std::vector<Message> &messageVectorOut)
{
    lock();
    messageVectorOut=messageVector;
    unlock();
    return 1;
}


int GlobalData::getCurrentRunMainFileName(QString &nameOut)
{
    lock();
//    if(currentRunFile.runingFileName.size()>0 )
//    {
//        nameOut = QString::fromStdString(currentRunFile.runingFileName[0]);
//    }
    if(currentRunFile.relatedFileName.size()>0 )
    {
        nameOut = QString::fromStdString(currentRunFile.relatedFileName[0]);
    }
    else
    {
        nameOut="";
    }

    unlock();
    return 1;
}

int GlobalData::setMapInfo( std::vector<PointCloudData_3D> &pointCloudIn)
{
    mapMutex.lock();
    pointCloud=pointCloudIn;
    mapMutex.unlock();
    return 1;
}

int GlobalData::getMapInfo(std::string &mapNameOut, std::vector<PointCloudData_3D> &pointCloudOut)
{
    mapMutex.lock();
    mapNameOut=mapName;
    pointCloudOut=pointCloud;
    mapMutex.unlock();
    return 1;
}

int GlobalData::getCurrentMapName(std::string &mapNameOut)
{
    mapMutex.lock();
    mapNameOut=mapName;
    mapMutex.unlock();
    return 1;
}

int GlobalData::getPointCloud(std::vector<PointCloudData_3D> &pointCloudOut)
{
    mapMutex.lock();
    pointCloudOut=pointCloud;
    mapMutex.unlock();
    return 1;
}

void GlobalData::lock()
{
    usingMutex.lock();
}

void GlobalData::unlock()
{
    usingMutex.unlock();
}

bool GlobalData::tryLock(int timeout)
{
    return usingMutex.tryLock(timeout);
}

bool GlobalData::tryLock()
{
    return usingMutex.tryLock();
}
